package net.documentshare.docu;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import net.simpleframework.ado.db.ExpressionValue;
import net.simpleframework.ado.db.ITableEntityManager;
import net.simpleframework.content.ContentUtils;
import net.simpleframework.core.ado.IDataObjectQuery;
import net.simpleframework.organization.IJob;
import net.simpleframework.util.ConvertUtils;
import net.simpleframework.util.StringUtils;
import net.simpleframework.web.page.component.ComponentParameter;
import net.simpleframework.web.page.component.ui.pager.AbstractTablePagerData;
import net.simpleframework.web.page.component.ui.pager.db.AbstractDbTablePagerHandle;

public class DocuDelLogTablePaperHandle extends AbstractDbTablePagerHandle {
	@Override
	public Object getBeanProperty(ComponentParameter compParameter, String beanProperty) {
		if ("jobExecute".equals(beanProperty)) {
			return IJob.sj_account_normal;
		}
		return super.getBeanProperty(compParameter, beanProperty);
	}

	@Override
	public IDataObjectQuery<?> createDataObjectQuery(ComponentParameter compParameter) {
		final ITableEntityManager tMgr = DocuUtils.applicationModule.getDataObjectManager(DocuDelLog.class);
		final List<Object> ol = new ArrayList<Object>();
		final StringBuffer sql = new StringBuffer();
		sql.append("1=1");
		final String userId = compParameter.getRequestParameter("userId");
		if (StringUtils.hasText(userId)) {
			sql.append(" and userId=?");
			ol.add(userId);
		}
		sql.append(" order by delDate desc");
		return tMgr.query(new ExpressionValue(sql.toString(), ol.toArray(new Object[] {})), DocuDelLog.class);
	}

	@Override
	public AbstractTablePagerData createTablePagerData(final ComponentParameter compParameter) {
		return new AbstractTablePagerData(compParameter) {

			@Override
			protected Map<Object, Object> getRowData(final Object arg0) {
				final DocuDelLog delLog = (DocuDelLog) arg0;
				final Map<Object, Object> rowData = new LinkedHashMap<Object, Object>();
				rowData.put("title", "<a onclick=\"$Actions['showDelLogWindowAct']('delLogId=" + delLog.getId() + "');\">" + delLog.getTitle()
						+ "</a>");
				rowData.put("userId", ContentUtils.getAccountAware().wrapAccountHref(compParameter, delLog.getUserId(), delLog.getUserText()));
				rowData.put("status", delLog.isStatus() ? "用户操作" : "管理员操作");
				rowData.put("delDate", ConvertUtils.toDateString(delLog.getDelDate(), "yyyy-MM-dd HH"));
				rowData.put("action", "<a class='dellogB down_menu_image'></a>");
				return rowData;
			}
		};
	}

	@Override
	public Map<String, Object> getFormParameters(ComponentParameter compParameter) {
		final Map<String, Object> parameters = super.getFormParameters(compParameter);
		putParameter(compParameter, parameters, "userId");
		return parameters;
	}

}
